﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using Rage;

namespace Rage.UI
{
    public partial class ItemButtonWithEnchant : UserControl
    {
        public ItemButtonWithEnchant()
        {
            // Required to initialize variables
            InitializeComponent();
            PopupUtilities.RegisterPopup(this, ListPopup, Close);
            PopupUtilities.RegisterPopup(this, ListPopupItemModType1, Close);
            PopupUtilities.RegisterPopup(this, ListPopupItemModType2, Close);
            PopupUtilities.RegisterPopup(this, ListPopupItemModType3, Close);
            PopupUtilities.RegisterPopup(this, ListPopupItemModType4, Close);
            PopupUtilities.RegisterPopup(this, ListPopupItemModType5, Close);
            UpdateButtonOptions();
        }

        private CharacterSlot slot;
        public CharacterSlot Slot
        {
            get { return slot; }
            set
            {
                slot = value;
                ComparisonItemList.Slot = slot;
                ComparisonItemListItemModType1.Slot = slot;
                ComparisonItemListItemModType2.Slot = slot;
                ComparisonItemListItemModType3.Slot = slot;
                ComparisonItemListItemModType4.Slot = slot;
                ComparisonItemListItemModType5.Slot = slot;
            }
        }

        public ItemInstance TheItem;
        private Character character;
        public Character Character
        {
            get { return character; }
            set
            {
                if (character != null) character.AvailableItemsChanged -= new Character.AvailableItemsChangedEventHandler(character_CalculationsInvalidated);
                character = value;
                DataContext = character;
                ComparisonItemList.Character = character;
                ComparisonItemListItemModType1.Character = character;
                ComparisonItemListItemModType2.Character = character;
                ComparisonItemListItemModType3.Character = character;
                ComparisonItemListItemModType4.Character = character;
                ComparisonItemListItemModType5.Character = character;
                if (character != null)
                {
                    character.CalculationsInvalidated += new EventHandler(character_CalculationsInvalidated);
                    character_CalculationsInvalidated(null, null);
                }
            }
        }

        private ItemInstance gear;
        public void character_CalculationsInvalidated(object sender, EventArgs e)
        {
            if (character != null)
            {
                TheItem = character[Slot];
                if (TheItem == null || TheItem.Item == null) {
                    IconImage.Source = null;
                    gear = null;
                    IconImageItemModType1.Source = null; ItemModTypeButton1.IsEnabled = false; SetItemModType(ItemModTypeButton1, ItemSlot.None);
                    IconImageItemModType2.Source = null; ItemModTypeButton2.IsEnabled = false; SetItemModType(ItemModTypeButton2, ItemSlot.None);
                    IconImageItemModType3.Source = null; ItemModTypeButton3.IsEnabled = false; SetItemModType(ItemModTypeButton3, ItemSlot.None);
                    IconImageItemModType4.Source = null; ItemModTypeButton4.IsEnabled = false; SetItemModType(ItemModTypeButton4, ItemSlot.None);
                    IconImageItemModType5.Source = null; ItemModTypeButton5.IsEnabled = false; SetItemModType(ItemModTypeButton5, ItemSlot.None);
                } else {
                    IconImage.Source = Icons.AnIcon(TheItem.Item.IconPath);
                    gear = TheItem;
                    // There are several special sockets, we need to account for them
                    bool AugSlot_Head = Slot == CharacterSlot.Head && Character.HeadAugmentSocketEnabled;
                    bool AugSlot_Chest = Slot == CharacterSlot.Chest && Character.ChestAugmentSocketEnabled;
                    bool AugSlot_Hands = Slot == CharacterSlot.Hands && Character.HandsAugmentSocketEnabled;
                    bool AugSlot_Legs = Slot == CharacterSlot.Legs && Character.LegsAugmentSocketEnabled;
                    bool AugSlot_Feet = Slot == CharacterSlot.Feet && Character.FeetAugmentSocketEnabled;
                    bool AugSlot_Ear = Slot == CharacterSlot.Ear && Character.EarAugmentSocketEnabled;
                    bool AugSlot_Implant1 = Slot == CharacterSlot.Implant1 && Character.Implant1AugmentSocketEnabled;
                    bool AugSlot_Implant2 = Slot == CharacterSlot.Implant2 && Character.Implant2AugmentSocketEnabled;
                    bool AugSlot_Wrist = Slot == CharacterSlot.Wrist && Character.WristAugmentSocketEnabled;
                    bool AugSlot_Relic1 = Slot == CharacterSlot.Relic1 && Character.Relic1AugmentSocketEnabled;
                    bool AugSlot_Relic2 = Slot == CharacterSlot.Relic2 && Character.Relic2AugmentSocketEnabled;
                    bool AugSlot_MainHand = Slot == CharacterSlot.MainHand && Character.MainHandAugmentSocketEnabled;
                    bool AugSlot_OffHand = Slot == CharacterSlot.OffHand && Character.OffHandAugmentSocketEnabled;
                    bool enableAugSlot = AugSlot_Head || AugSlot_Chest || AugSlot_Hands || AugSlot_Legs || AugSlot_Feet
                         || AugSlot_Ear || AugSlot_Implant1 || AugSlot_Implant2 || AugSlot_Wrist || AugSlot_Relic1
                          || AugSlot_Relic2 || AugSlot_MainHand || AugSlot_OffHand;
                    // If there is no gem socket there, hide the selector
                    ItemModTypeButton1.IsEnabled = TheItem.Item.ItemArmoringHiltBarrelType != ItemSlot.None;
                    ItemModTypeButton2.IsEnabled = TheItem.Item.ItemModType != ItemSlot.None;
                    ItemModTypeButton3.IsEnabled = TheItem.Item.ItemEnhancementType != ItemSlot.None;
                    ItemModTypeButton4.IsEnabled = TheItem.Item.ItemColorCrystalType != ItemSlot.None;
                    ItemModTypeButton5.IsEnabled = TheItem.Item.ItemAugmentType != ItemSlot.None || enableAugSlot;
                    // Use the ItemModType's image if it exists
                    IconImageItemModType1.Source = TheItem.ItemMod1 != null ? Icons.AnIcon(TheItem.ItemMod1.IconPath) : null;
                    IconImageItemModType2.Source = TheItem.ItemMod2 != null ? Icons.AnIcon(TheItem.ItemMod2.IconPath) : null;
                    IconImageItemModType3.Source = TheItem.ItemMod3 != null ? Icons.AnIcon(TheItem.ItemMod3.IconPath) : null;
                    IconImageItemModType4.Source = TheItem.ItemMod4 != null ? Icons.AnIcon(TheItem.ItemMod4.IconPath) : null;
                    IconImageItemModType5.Source = TheItem.ItemMod5 != null ? Icons.AnIcon(TheItem.ItemMod5.IconPath) : null;
                    // Remove any previously registered Events that can be fired
                    ComparisonItemListItemModType1.SelectedItemsItemModTypeChanged -= new EventHandler(ComparisonItemListItemModType1_SelectedItemsItemModTypeChanged);
                    ComparisonItemListItemModType2.SelectedItemsItemModTypeChanged -= new EventHandler(ComparisonItemListItemModType2_SelectedItemsItemModTypeChanged);
                    ComparisonItemListItemModType3.SelectedItemsItemModTypeChanged -= new EventHandler(ComparisonItemListItemModType3_SelectedItemsItemModTypeChanged);
                    ComparisonItemListItemModType4.SelectedItemsItemModTypeChanged -= new EventHandler(ComparisonItemListItemModType4_SelectedItemsItemModTypeChanged);
                    ComparisonItemListItemModType5.SelectedItemsItemModTypeChanged -= new EventHandler(ComparisonItemListItemModType5_SelectedItemsItemModTypeChanged);
                    // Set up the selector so that it uses the right slot type
                    ComparisonItemListItemModType1.ItemModTypeIndex = 1;
                    ComparisonItemListItemModType2.ItemModTypeIndex = 2;
                    ComparisonItemListItemModType3.ItemModTypeIndex = 3;
                    ComparisonItemListItemModType4.ItemModTypeIndex = 4;
                    ComparisonItemListItemModType5.ItemModTypeIndex = 5;
                    ComparisonItemListItemModType1.ItemModTypeSlot = GetProperItemModTypeSlot(TheItem.Item.ItemArmoringHiltBarrelType);
                    ComparisonItemListItemModType2.ItemModTypeSlot = GetProperItemModTypeSlot(TheItem.Item.ItemModType);
                    ComparisonItemListItemModType3.ItemModTypeSlot = GetProperItemModTypeSlot(TheItem.Item.ItemEnhancementType);
                    ComparisonItemListItemModType4.ItemModTypeSlot = GetProperItemModTypeSlot(TheItem.Item.ItemColorCrystalType);
                    ComparisonItemListItemModType5.ItemModTypeSlot = GetProperItemModTypeSlot(TheItem.Item.ItemAugmentType);
                    // Update the socket colors
                    SetItemModTypes();
                    // Set the ItemModType lists' selected items to the core item's gems
                    ComparisonItemListItemModType1.SelectedItem = TheItem.ItemMod1;
                    ComparisonItemListItemModType2.SelectedItem = TheItem.ItemMod2;
                    ComparisonItemListItemModType3.SelectedItem = TheItem.ItemMod3;
                    ComparisonItemListItemModType4.SelectedItem = TheItem.ItemMod4;
                    ComparisonItemListItemModType5.SelectedItem = TheItem.ItemMod5;
                    // Let the comp lists know they need to behave differently from a normal list
                    ComparisonItemListItemModType1.IsAnItemsItemModType = true;
                    ComparisonItemListItemModType2.IsAnItemsItemModType = true;
                    ComparisonItemListItemModType3.IsAnItemsItemModType = true;
                    ComparisonItemListItemModType4.IsAnItemsItemModType = true;
                    ComparisonItemListItemModType5.IsAnItemsItemModType = true;
                    // Register the Events that can be fired
                    ComparisonItemListItemModType1.SelectedItemsItemModTypeChanged += new EventHandler(ComparisonItemListItemModType1_SelectedItemsItemModTypeChanged);
                    ComparisonItemListItemModType2.SelectedItemsItemModTypeChanged += new EventHandler(ComparisonItemListItemModType2_SelectedItemsItemModTypeChanged);
                    ComparisonItemListItemModType3.SelectedItemsItemModTypeChanged += new EventHandler(ComparisonItemListItemModType3_SelectedItemsItemModTypeChanged);
                    ComparisonItemListItemModType4.SelectedItemsItemModTypeChanged += new EventHandler(ComparisonItemListItemModType4_SelectedItemsItemModTypeChanged);
                    ComparisonItemListItemModType5.SelectedItemsItemModTypeChanged += new EventHandler(ComparisonItemListItemModType5_SelectedItemsItemModTypeChanged);
                }
            }
        }

        private CharacterSlot GetProperItemModTypeSlot(ItemSlot s) {
            return (s == ItemSlot.Armoring ? CharacterSlot.Armoring :
                   (s == ItemSlot.Barrel ? CharacterSlot.Barrel :
                   (s == ItemSlot.Hilt ? CharacterSlot.Hilt :
                   (s == ItemSlot.Mod ? CharacterSlot.Mod :
                   (s == ItemSlot.Enhancement ? CharacterSlot.Enhancement :
                   (s == ItemSlot.ColorCrystal ? CharacterSlot.ColorCrystal :
                   (s == ItemSlot.Augment ? CharacterSlot.Augment :
                   (CharacterSlot.Augment))))))));
        }

        private void SetItemModType(Button button, ItemSlot slot) {
            Color c = ItemTooltip.ColorForMod(slot);
            if (c == Colors.Transparent) { c = SystemColors.ControlColor; }
            Brush b = new SolidColorBrush(ItemTooltip.ColorForMod(slot));
            button.Background = b;
            button.BorderBrush = b;
            button.BorderThickness = new Thickness(2);
        }
        private void SetItemModTypes()
        {
            SetItemModType(ItemModTypeButton1, TheItem.Item.ItemArmoringHiltBarrelType);
            SetItemModType(ItemModTypeButton2, TheItem.Item.ItemModType);
            SetItemModType(ItemModTypeButton3, TheItem.Item.ItemEnhancementType);
            SetItemModType(ItemModTypeButton4, TheItem.Item.ItemColorCrystalType);
            SetItemModType(ItemModTypeButton5, TheItem.Item.ItemAugmentType != ItemSlot.None ? TheItem.Item.ItemAugmentType : ((GetWhichIsAugSocket() == 5) ? ItemSlot.Augment : ItemSlot.None));
        }

        private int GetWhichIsAugSocket()
        {
            int retVal = -1;
            //
            bool AugSlot_Head = Slot == CharacterSlot.Head && Character.HeadAugmentSocketEnabled;
            bool AugSlot_Chest = Slot == CharacterSlot.Chest && Character.ChestAugmentSocketEnabled;
            bool AugSlot_Hands = Slot == CharacterSlot.Hands && Character.HandsAugmentSocketEnabled;
            bool AugSlot_Legs = Slot == CharacterSlot.Legs && Character.LegsAugmentSocketEnabled;
            bool AugSlot_Feet = Slot == CharacterSlot.Feet && Character.FeetAugmentSocketEnabled;
            bool AugSlot_Ear = Slot == CharacterSlot.Ear && Character.EarAugmentSocketEnabled;
            bool AugSlot_Implant1 = Slot == CharacterSlot.Implant1 && Character.Implant1AugmentSocketEnabled;
            bool AugSlot_Implant2 = Slot == CharacterSlot.Implant2 && Character.Implant2AugmentSocketEnabled;
            bool AugSlot_Wrist = Slot == CharacterSlot.Wrist && Character.WristAugmentSocketEnabled;
            bool AugSlot_Relic1 = Slot == CharacterSlot.Relic1 && Character.Relic1AugmentSocketEnabled;
            bool AugSlot_Relic2 = Slot == CharacterSlot.Relic2 && Character.Relic2AugmentSocketEnabled;
            bool AugSlot_MainHand = Slot == CharacterSlot.MainHand && Character.MainHandAugmentSocketEnabled;
            bool AugSlot_OffHand = Slot == CharacterSlot.OffHand && Character.OffHandAugmentSocketEnabled;
            bool enable = AugSlot_Head || AugSlot_Chest || AugSlot_Hands || AugSlot_Legs || AugSlot_Feet
                 || AugSlot_Ear || AugSlot_Implant1 || AugSlot_Implant2 || AugSlot_Wrist || AugSlot_Relic1
                  || AugSlot_Relic2 || AugSlot_MainHand || AugSlot_OffHand;
            // If there is no mod socket there, hide the selector
            if (enable) { retVal = 5; }
            //
            return retVal;
        }
        private void Close()
        {
            if (ListPopup.IsOpen) { ListPopup.IsOpen = false; }
            if (ListPopupItemModType1.IsOpen) { ListPopupItemModType1.IsOpen = false; }
            if (ListPopupItemModType2.IsOpen) { ListPopupItemModType2.IsOpen = false; }
            if (ListPopupItemModType3.IsOpen) { ListPopupItemModType3.IsOpen = false; }
            if (ListPopupItemModType4.IsOpen) { ListPopupItemModType4.IsOpen = false; }
            if (ListPopupItemModType5.IsOpen) { ListPopupItemModType5.IsOpen = false; }
        }

        #region Popup Lists
        private void EnsureItemModType1PopupsVisible()
        {
            GeneralTransform transform = TransformToVisual(App.Current.RootVisual);
            double distBetweenBottomOfPopupAndBottomOfWindow = App.Current.RootVisual.RenderSize.Height - transform.Transform(new Point(0, ComparisonItemListItemModType1.Height)).Y;
            if (distBetweenBottomOfPopupAndBottomOfWindow < 0)
            {
                ListPopupItemModType1.VerticalOffset = distBetweenBottomOfPopupAndBottomOfWindow;
            }
        }
        private void EnsureItemModType2PopupsVisible()
        {
            GeneralTransform transform = TransformToVisual(App.Current.RootVisual);
            double distBetweenBottomOfPopupAndBottomOfWindow = App.Current.RootVisual.RenderSize.Height - transform.Transform(new Point(0, ComparisonItemListItemModType2.Height)).Y;
            if (distBetweenBottomOfPopupAndBottomOfWindow < 0)
            {
                ListPopupItemModType2.VerticalOffset = distBetweenBottomOfPopupAndBottomOfWindow;
            }
        }
        private void EnsureItemModType3PopupsVisible()
        {
            GeneralTransform transform = TransformToVisual(App.Current.RootVisual);
            double distBetweenBottomOfPopupAndBottomOfWindow = App.Current.RootVisual.RenderSize.Height - transform.Transform(new Point(0, ComparisonItemListItemModType3.Height)).Y;
            if (distBetweenBottomOfPopupAndBottomOfWindow < 0)
            {
                ListPopupItemModType3.VerticalOffset = distBetweenBottomOfPopupAndBottomOfWindow;
            }
        }
        private void EnsureItemModType4PopupsVisible()
        {
            GeneralTransform transform = TransformToVisual(App.Current.RootVisual);
            double distBetweenBottomOfPopupAndBottomOfWindow = App.Current.RootVisual.RenderSize.Height - transform.Transform(new Point(0, ComparisonItemListItemModType4.Height)).Y;
            if (distBetweenBottomOfPopupAndBottomOfWindow < 0)
            {
                ListPopupItemModType4.VerticalOffset = distBetweenBottomOfPopupAndBottomOfWindow;
            }
        }
        private void EnsureItemModType5PopupsVisible()
        {
            GeneralTransform transform = TransformToVisual(App.Current.RootVisual);
            double distBetweenBottomOfPopupAndBottomOfWindow = App.Current.RootVisual.RenderSize.Height - transform.Transform(new Point(0, ComparisonItemListItemModType5.Height)).Y;
            if (distBetweenBottomOfPopupAndBottomOfWindow < 0)
            {
                ListPopupItemModType5.VerticalOffset = distBetweenBottomOfPopupAndBottomOfWindow;
            }
        }

        #region Context Menu
        private void MainButton_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            //TODO: *Usually* this is enough, but sometimes the positioning gets off, and it stays where the last context menu was.
            //Not really sure why, but I guess we should set the context menu location here too?
            ContextMenuService.GetContextMenu(MainButton).IsOpen = true;
            if (e != null) { e.Handled = true; }
        }
        private void ContextMenuItem_Opened(object sender, RoutedEventArgs e) {
            if (TheItem == null) { ContextMenuItem.IsOpen = false; }
            else { ContextItemName.Header = TheItem.Name; }
        }
        private void EditItem(object sender, RoutedEventArgs e)
        {
            new ItemEditor() { CurrentItem = TheItem.Item }.Show();
        }
#if SILVERLIGHT
        public class MyHyperlinkButton : HyperlinkButton
        {
            public MyHyperlinkButton(string navigateUri)
            {
                base.NavigateUri = new Uri(navigateUri);
                TargetName = "_blank";
            }
            public void ClickMe() { base.OnClick(); }
        }
#endif
        private void OpenInKnotor(object sender, RoutedEventArgs e)
        {
#if SILVERLIGHT
            if (App.Current.IsRunningOutOfBrowser) {
                MyHyperlinkButton button = new MyHyperlinkButton("http://knotor.com/items/" + TheItem.Id);
                button.ClickMe();
            } else {
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("http://knotor.com/items/" + TheItem.Id), "_blank");
            }
#else
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("http://knotor.com/items/" + TheItem.Id));
#endif
        }
        private void RefreshItemFromKnotor(object sender, RoutedEventArgs e)
        {
            Item.LoadFromId(TheItem.Id, true, true, true, false);
        }
        #endregion
        private void MainButton_Clicked(object sender, RoutedEventArgs e)
        {
            MainPage.Tooltip.Hide();
#if SILVERLIGHT
            EnsurePopupsVisible();
#else
            ListPopup.PlacementTarget = LayoutRoot;
            ListPopup.Placement = PlacementMode.RelativePoint;
#endif
            ComparisonItemList.IsShown = true;
            ListPopup.IsOpen = true;
            ComparisonItemList.Focus();
        }
        private void ItemModTypeButton1_Click(object sender, RoutedEventArgs e)
        {
            MainPage.Tooltip.Hide();
#if SILVERLIGHT
            EnsureItemModType1PopupsVisible();
#else
            ListPopupItemModType1.PlacementTarget = ItemModTypeButton1;
            ListPopupItemModType1.Placement = PlacementMode.RelativePoint;
#endif
            ComparisonItemListItemModType1.IsShown = true;
            ListPopupItemModType1.IsOpen = true;
            ComparisonItemListItemModType1.Focus();
        }
        private void ItemModTypeButton2_Click(object sender, RoutedEventArgs e)
        {
            MainPage.Tooltip.Hide();
#if SILVERLIGHT
            EnsureItemModType2PopupsVisible();
#else
            ListPopupItemModType2.PlacementTarget = ItemModTypeButton2;
            ListPopupItemModType2.Placement = PlacementMode.RelativePoint;
#endif
            ComparisonItemListItemModType2.IsShown = true;
            ListPopupItemModType2.IsOpen = true;
            ComparisonItemListItemModType2.Focus();
        }
        private void ItemModTypeButton3_Click(object sender, RoutedEventArgs e)
        {
            MainPage.Tooltip.Hide();
#if SILVERLIGHT
            EnsureItemModType3PopupsVisible();
#else
            ListPopupItemModType3.PlacementTarget = ItemModTypeButton3;
            ListPopupItemModType3.Placement = PlacementMode.RelativePoint;
#endif
            ComparisonItemListItemModType3.IsShown = true;
            ListPopupItemModType3.IsOpen = true;
            ComparisonItemListItemModType3.Focus();
        }
        private void ItemModTypeButton4_Click(object sender, RoutedEventArgs e)
        {
            MainPage.Tooltip.Hide();
#if SILVERLIGHT
            EnsureItemModType4PopupsVisible();
#else
            ListPopupItemModType4.PlacementTarget = ItemModTypeButton4;
            ListPopupItemModType4.Placement = PlacementMode.RelativePoint;
#endif
            ComparisonItemListItemModType4.IsShown = true;
            ListPopupItemModType4.IsOpen = true;
            ComparisonItemListItemModType4.Focus();
        }
        private void ItemModTypeButton5_Click(object sender, RoutedEventArgs e)
        {
            MainPage.Tooltip.Hide();
#if SILVERLIGHT
            EnsureItemModType5PopupsVisible();
#else
            ListPopupItemModType5.PlacementTarget = ItemModTypeButton5;
            ListPopupItemModType5.Placement = PlacementMode.RelativePoint;
#endif
            ComparisonItemListItemModType5.IsShown = true;
            ListPopupItemModType5.IsOpen = true;
            ComparisonItemListItemModType5.Focus();
        }
        #endregion

        #region Returning ItemModType Events
        public void ComparisonItemListItemModType1_SelectedItemsItemModTypeChanged(object sender, EventArgs e) { if (TheItem != null) TheItem.ItemMod1 = ComparisonItemListItemModType1.SelectedItem; character.OnCalculationsInvalidated(); character_CalculationsInvalidated(null, null); }
        public void ComparisonItemListItemModType2_SelectedItemsItemModTypeChanged(object sender, EventArgs e) { if (TheItem != null) TheItem.ItemMod2 = ComparisonItemListItemModType2.SelectedItem; character.OnCalculationsInvalidated(); character_CalculationsInvalidated(null, null); }
        public void ComparisonItemListItemModType3_SelectedItemsItemModTypeChanged(object sender, EventArgs e) { if (TheItem != null) TheItem.ItemMod3 = ComparisonItemListItemModType3.SelectedItem; character.OnCalculationsInvalidated(); character_CalculationsInvalidated(null, null); }
        public void ComparisonItemListItemModType4_SelectedItemsItemModTypeChanged(object sender, EventArgs e) { if (TheItem != null) TheItem.ItemMod4 = ComparisonItemListItemModType4.SelectedItem; character.OnCalculationsInvalidated(); character_CalculationsInvalidated(null, null); }
        public void ComparisonItemListItemModType5_SelectedItemsItemModTypeChanged(object sender, EventArgs e) { if (TheItem != null) TheItem.ItemMod5 = ComparisonItemListItemModType5.SelectedItem; character.OnCalculationsInvalidated(); character_CalculationsInvalidated(null, null); }
        #endregion

        #region ToolTips
        private void MainButton_MouseEnter(object sender, MouseEventArgs e)
        {
            if (!ListPopup.IsOpen)
            {
                MainPage.Tooltip.ItemInstance = gear;
                MainPage.Tooltip.Show(MainButton, 72, 0);
            }
        }
        private void ItemModTypeButton1_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (!ListPopup.IsOpen && !ListPopupItemModType1.IsOpen && !ListPopupItemModType2.IsOpen && !ListPopupItemModType3.IsOpen && !ListPopupItemModType4.IsOpen && !ListPopupItemModType5.IsOpen)
                {
                    MainPage.Tooltip.Item = TheItem.ItemMod1;
                    MainPage.Tooltip.Show(ItemModTypeButton1, 22, 0);
                }
            }
            catch (NullReferenceException /*ex*/)
            {
                // sometimes this throws NullReferenceException, but unable to determine what is null
            }
        }
        private void ItemModTypeButton2_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (!ListPopup.IsOpen && !ListPopupItemModType1.IsOpen && !ListPopupItemModType2.IsOpen && !ListPopupItemModType3.IsOpen && !ListPopupItemModType4.IsOpen && !ListPopupItemModType5.IsOpen)
                {
                    MainPage.Tooltip.Item = TheItem.ItemMod2;
                    MainPage.Tooltip.Show(ItemModTypeButton2, 22, 0);
                }
            }
            catch (NullReferenceException /*ex*/)
            {
                // sometimes this throws NullReferenceException, but unable to determine what is null
            }
        }
        private void ItemModTypeButton3_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (!ListPopup.IsOpen && !ListPopupItemModType1.IsOpen && !ListPopupItemModType2.IsOpen && !ListPopupItemModType3.IsOpen && !ListPopupItemModType4.IsOpen && !ListPopupItemModType5.IsOpen)
                {
                    MainPage.Tooltip.Item = TheItem.ItemMod3;
                    MainPage.Tooltip.Show(ItemModTypeButton3, 22, 0);
                }
            }
            catch (NullReferenceException /*ex*/)
            {
                // sometimes this throws NullReferenceException, but unable to determine what is null
            }
        }
        private void ItemModTypeButton4_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (!ListPopup.IsOpen && !ListPopupItemModType1.IsOpen && !ListPopupItemModType2.IsOpen && !ListPopupItemModType3.IsOpen && !ListPopupItemModType4.IsOpen && !ListPopupItemModType5.IsOpen)
                {
                    MainPage.Tooltip.Item = TheItem.ItemMod4;
                    MainPage.Tooltip.Show(ItemModTypeButton4, 22, 0);
                }
            }
            catch (NullReferenceException /*ex*/)
            {
                // sometimes this throws NullReferenceException, but unable to determine what is null
            }
        }
        private void ItemModTypeButton5_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (!ListPopup.IsOpen && !ListPopupItemModType1.IsOpen && !ListPopupItemModType2.IsOpen && !ListPopupItemModType3.IsOpen && !ListPopupItemModType4.IsOpen && !ListPopupItemModType5.IsOpen)
                {
                    MainPage.Tooltip.Item = TheItem.ItemMod5;
                    MainPage.Tooltip.Show(ItemModTypeButton5, 22, 0);
                }
            }
            catch (NullReferenceException /*ex*/)
            {
                // sometimes this throws NullReferenceException, but unable to determine what is null
            }
        }

        private void AnyButton_MouseLeave(object sender, MouseEventArgs e) { MainPage.Tooltip.Hide(); }
        #endregion

        public void CK_Augment_Checked(object sender, RoutedEventArgs e)
        {
            MainPage.Tooltip.Hide();
            AugSocketIsChecked = CK_Augment.IsChecked.GetValueOrDefault(false);
        }

        private bool _ShowItemModType1 = true; public bool ShowItemModType1 { get { return _ShowItemModType1; } set { _ShowItemModType1 = value; UpdateButtonOptions(); } }
        private bool _ShowItemModType2 = true; public bool ShowItemModType2 { get { return _ShowItemModType2; } set { _ShowItemModType2 = value; UpdateButtonOptions(); } }
        private bool _ShowItemModType3 = true; public bool ShowItemModType3 { get { return _ShowItemModType3; } set { _ShowItemModType3 = value; UpdateButtonOptions(); } }
        private bool _ShowItemModType4 = true; public bool ShowItemModType4 { get { return _ShowItemModType4; } set { _ShowItemModType4 = value; UpdateButtonOptions(); } }
        private bool _ShowItemModType5 = true; public bool ShowItemModType5 { get { return _ShowItemModType5; } set { _ShowItemModType5 = value; UpdateButtonOptions(); } }
        private bool _EnableAugSocket = true; public bool EnableAugSocket { get { return _EnableAugSocket; } set { _EnableAugSocket = value; UpdateButtonOptions(); } }

        public Boolean AugSocketIsChecked {
            get { return (Boolean)this.GetValue(AugSocketProperty); }
            set {
                // Set the Value
                this.SetValue(AugSocketProperty, value);
                // Update the socket on the item
                if (Slot == CharacterSlot.Head && Character.HeadAugmentSocketEnabled != value) { Character.HeadAugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.Chest && Character.ChestAugmentSocketEnabled != value) { Character.ChestAugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.Hands && Character.HandsAugmentSocketEnabled != value) { Character.HandsAugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.Waist && Character.WaistAugmentSocketEnabled != value) { Character.WaistAugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.Legs && Character.LegsAugmentSocketEnabled != value) { Character.LegsAugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.Feet && Character.FeetAugmentSocketEnabled != value) { Character.FeetAugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.Ear && Character.EarAugmentSocketEnabled != value) { Character.EarAugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.Implant1 && Character.Implant1AugmentSocketEnabled != value) { Character.Implant1AugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.Implant2 && Character.Implant2AugmentSocketEnabled != value) { Character.Implant2AugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.Wrist && Character.WristAugmentSocketEnabled != value) { Character.WristAugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.Relic1 && Character.Relic1AugmentSocketEnabled != value) { Character.Relic1AugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.Relic2 && Character.Relic2AugmentSocketEnabled != value) { Character.Relic2AugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.MainHand && Character.MainHandAugmentSocketEnabled != value) { Character.MainHandAugmentSocketEnabled = value; }
                else if (Slot == CharacterSlot.OffHand && Character.OffHandAugmentSocketEnabled != value) { Character.OffHandAugmentSocketEnabled = value; }
                // Update the Checkbox, if need be. The if is to prevent stack overflow
                if (CK_Augment.IsChecked != value) { CK_Augment.IsChecked = value; }
            }
        }
        public static readonly DependencyProperty AugSocketProperty = DependencyProperty.Register(
            "AugSocketIsChecked", typeof(Boolean), typeof(ItemButtonWithEnchant), new PropertyMetadata(false));

        public void SetButtonOptions(bool showItemModType1, bool showItemModType2, bool showItemModType3, bool showItemModType4, bool showItemModType5)
        {
            _ShowItemModType1 = showItemModType1;
            _ShowItemModType2 = showItemModType2;
            _ShowItemModType3 = showItemModType3;
            _ShowItemModType4 = showItemModType4;
            _ShowItemModType5 = showItemModType5;
            //
            UpdateButtonOptions();
        }
        public void UpdateButtonOptions() {
            ItemModTypeButton1.Visibility = ShowItemModType1 ? Visibility.Visible : Visibility.Collapsed;
            ItemModTypeButton2.Visibility = ShowItemModType2 ? Visibility.Visible : Visibility.Collapsed;
            ItemModTypeButton3.Visibility = ShowItemModType3 ? Visibility.Visible : Visibility.Collapsed;
            ItemModTypeButton4.Visibility = ShowItemModType4 ? Visibility.Visible : Visibility.Collapsed;
            ItemModTypeButton5.Visibility = ShowItemModType5 ? Visibility.Visible : Visibility.Collapsed;
            if (CK_Augment.IsChecked != AugSocketIsChecked)
            {
                CK_Augment.IsChecked = AugSocketIsChecked;
            }
        }
    }
}
